package cz.zcu.fav.ratdroid.activity.calendar;

import java.io.Serializable;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.caldroid.CaldroidListener;

import cz.zcu.fav.ratdroid.R;
import cz.zcu.fav.ratdroid.activity.SkeletonRestFragmentActivity;
import cz.zcu.fav.ratdroid.activity.calendar.day.DayActivity;
import cz.zcu.fav.ratdroid.activity.calendar.provider.ICalendarDataProvider;
import cz.zcu.fav.ratdroid.bean.Occupations;

public class CalendarActivity extends SkeletonRestFragmentActivity<Occupations>
{
	public static int INVALIDATE_CALENDAR_RESULT_KEY = 3;
	private CaldroidRATFragment caldroidFragment;
	private Occupations dates;
	private ICalendarDataProvider asyncDataProvider;
	private static String LOADED_DATES_KEY = "loaded.dates";
	private DateTime lastSelectedDate;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		boolean recreated = false;
		if (savedInstanceState != null)
		{
			recreated = true;
			dates = (Occupations) savedInstanceState
					.getSerializable(LOADED_DATES_KEY);
		}

		asyncDataProvider = (ICalendarDataProvider) getIntent()
				.getSerializableExtra(
						ICalendarDataProvider.CALENDAR_DATA_PROVIDER_IDENTIFIER);

		setContentView(R.layout.activity_main);
		caldroidFragment = new CaldroidRATFragment();
		caldroidFragment.setDates(dates);

		Bundle args = new Bundle();
		DateTime now = DateTime.now();
		args.putInt("month", now.getMonthOfYear());
		args.putInt("year", now.getYear());
		lastSelectedDate = now;
		caldroidFragment.setArguments(args);

		FragmentTransaction t = getSupportFragmentManager().beginTransaction();
		t.replace(R.id.calendar1, caldroidFragment);
		t.commit();

		// Setup listener
		final CaldroidListener listener = new CaldroidListener()
		{

			@Override
			public void onSelectDate(Date date, View view)
			{
				lastSelectedDate = new DateTime(date);
				Intent fav = new Intent(CalendarActivity.this,
						DayActivity.class);
				fav.putExtra(DayActivity.PRESELECTED_DATE, new LocalDate(date));
				fav.putExtra(
						ICalendarDataProvider.CALENDAR_DATA_PROVIDER_IDENTIFIER,
						asyncDataProvider);
				startActivity(fav);
			}

			@Override
			public void onChangeMonth(int month, int year)
			{
				setRestTask(asyncDataProvider.getDatesOccupation(CalendarActivity.this,
						month, year, CalendarActivity.this));
			}

		};
		caldroidFragment.setCaldroidListener(listener);

		if (!recreated)
		{
			setRestTask(asyncDataProvider.getDatesOccupation(CalendarActivity.this,
					DateTime.now().getMonthOfYear(), DateTime.now().getYear(),
					CalendarActivity.this));
		}
	}


	public void notifyDataLoaded(Occupations data)
	{
		if (data != null)
		{
			this.dates = data;
			caldroidFragment.setDates(data);
		}
		caldroidFragment.refreshView();
	}

	@Override
	public void onSaveInstanceState(Bundle savedInstanceState)
	{
		// Save the user's current game state
		savedInstanceState.putSerializable(LOADED_DATES_KEY,
				(Serializable) dates);
		// Always call the superclass so it can save the view hierarchy state
		super.onSaveInstanceState(savedInstanceState);
	}
}
